#!/usr/bin/env python
#coding:utf-8

import wx
import types
import threading
import time

class ListPanel(wx.Panel):
    
    popmenulist = [(None, #parent menu id
        [
            (180, 'IDPM_CLEAR', u'Clear Shell Window' + '\tCtrl+Alt+R', wx.ITEM_NORMAL, 'OnClearShell', u'Clears content of shell window.'),
            
        ]),
    ]
    
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        
        sizer1 = wx.BoxSizer(wx.VERTICAL)
        
        self.list = wx.ListCtrl(self, style = wx.LC_REPORT)
        self.list.InsertColumn(0, u"时间", width = 150)
        self.list.InsertColumn(1, u"信息", width = 1200)
        sizer1.Add(self.list, 1, wx.EXPAND | wx.ALL, 2)
        
        self.SetSizer(sizer1)
        
    def Append(self, timestr, msg):
        item = self.list.InsertStringItem(self.list.GetItemCount(), timestr)
        self.list.SetStringItem(item, 1, msg)
        self.list.ScrollLines(self.list.GetItemCount())
        
class TextPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        
        sizer1 = wx.BoxSizer(wx.VERTICAL)
        
        self.text = wx.TextCtrl(self, style = wx.TE_READONLY | wx.TE_MULTILINE)
        sizer1.Add(self.text, 1, wx.EXPAND | wx.ALL, 2)
        
        self.SetSizer(sizer1) 
        
    def Append(self, msg):
        self.text.AppendText(msg + '\n')
        
class BlinkThread(threading.Thread):
    def __init__(self, notebook, pageid):
        threading.Thread.__init__(self)
        self.nb = notebook
        self.page = pageid
        self.image = self.nb.GetPageImage(pageid)
        self.enable = False
        
    def start(self):
        self.enable = True
        threading.Thread.start(self)
        
    def stop(self):
        self.enable = False
        
    def run(self):
        while self.enable:
            if not self.nb:
                return
            wx.CallAfter(self.nb.SetPageImage, self.page, 5)
            if not self.enable and not self.nb:
                wx.CallAfter(self.nb.SetPageImage, self.page, self.image)
                return
            time.sleep(0.5)
            if self.enable and not self.nb:
                wx.CallAfter(self.nb.SetPageImage, self.page, self.image)
            if not self.enable:
                return
            #time.sleep(0.5)
            if not self.enable:
                return
            #time.sleep(0.5)

class LogPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        
        sizer1 = wx.BoxSizer(wx.VERTICAL)
        
        self.notebook = wx.Notebook(self)
        isz = (16,16)
        il = wx.ImageList(isz[0], isz[1])
        il.Add(wx.Bitmap('images/TortoiseDeleted.png'))
        il.Add(wx.Bitmap('images/TortoiseConflict.png'))
        il.Add(wx.Bitmap('images/debug.png'))
        il.Add(wx.Bitmap('images/TortoiseModified.png'))
        il.Add(wx.Bitmap('images/minus.png'))
        il.Add(wx.Bitmap('images/uncheck.gif'))
        self.notebook.AssignImageList(il)
        
        self.errorpanel = ListPanel(self.notebook)
        self.notebook.AddPage(self.errorpanel, u"错误", imageId = 0)
        self.warnpanel = ListPanel(self.notebook)
        self.notebook.AddPage(self.warnpanel, u"警告", imageId = 1)
        self.debugpanel = ListPanel(self.notebook)
        self.notebook.AddPage(self.debugpanel, u"调试", imageId = 2)
        self.infopanel = ListPanel(self.notebook)
        self.notebook.AddPage(self.infopanel, u"信息", imageId = 3)
        self.tracepanel = TextPanel(self.notebook)
        self.notebook.AddPage(self.tracepanel, u"Traceback", imageId = 4)
        
        sizer1.Add(self.notebook, 1, wx.EXPAND | wx.ALL, 2)
        
        self.SetSizer(sizer1)
        
        self.error_blink = None
        self.warn_blink = None
        self.debug_blink = None
        self.info_blink = None
        self.trace_blink = None
        
        self.notebook.Bind(wx.EVT_NOTEBOOK_PAGE_CHANGED, self.OnPageChanged)
        
    def __del__(self):
        self.StopBlink()
            
    def StopBlink(self):
        if self.error_blink:
            self.error_blink.stop()
            self.error_blink.join()
            self.error_blink = None
            
        if self.warn_blink:
            self.warn_blink.stop()
            self.warn_blink.join()
            self.warn_blink = None
            
        if self.debug_blink:
            self.debug_blink.stop()
            self.debug_blink.join()
            self.debug_blink = None
            
        if self.info_blink:
            self.info_blink.stop()
            self.info_blink.join()
            self.info_blink = None
            
        if self.trace_blink:
            self.trace_blink.stop()
            self.trace_blink.join()
            self.trace_blink = None
        
        
    def Error(self, timestr, *args):
        self.errorpanel.Append(timestr, self.MakeMsg(*args))
        if self.notebook.GetSelection() != 0:
            if not self.error_blink:
                self.error_blink = BlinkThread(self.notebook, 0)
                self.error_blink.start()
    
    def Warn(self, timestr, *args):
        self.warnpanel.Append(timestr, self.MakeMsg(*args))
        if self.notebook.GetSelection() != 1:
            if not self.warn_blink:
                self.warn_blink = BlinkThread(self.notebook, 1)
                self.warn_blink.start()
    
    def Debug(self, timestr, *args):
        self.debugpanel.Append(timestr, self.MakeMsg(*args))
        if self.notebook.GetSelection() != 2:
            if not self.debug_blink:
                self.debug_blink = BlinkThread(self.notebook, 2)
                self.debug_blink.start()
    
    def Info(self, timestr, *args):
        self.infopanel.Append(timestr, self.MakeMsg(*args))
        if self.notebook.GetSelection() != 3:
            if not self.info_blink:
                self.info_blink = BlinkThread(self.notebook, 3)
                self.info_blink.start()
    
    def Traceback(self, msg):
        if not self:
            return
        self.tracepanel.Append(msg)
        if self.notebook.GetSelection() != 4:
            if not self.trace_blink:
                self.trace_blink = BlinkThread(self.notebook, 4)
                self.trace_blink.start()
    
    def MakeMsg(self, *args):
        msg = []
        encoding = 'utf-8'
        for i in args:
            if not type(i) in [types.UnicodeType, types.StringTypes, types.StringType]:
                s= repr(i)
            else:
                s = i
            if type(s) == type(u''):
                msg.append(s.encode(encoding))
            else:
                msg.append(s)
        return u''.join(msg)
    
    def OnPageChanged(self, evt):
        sel = evt.GetSelection()
        if sel == 0:
            if self.error_blink:
                self.error_blink.stop()
                self.error_blink.join()
                self.error_blink = None
        elif sel == 1:
            if self.warn_blink:
                self.warn_blink.stop()
                self.warn_blink.join()
                self.warn_blink = None
        elif sel == 2:
            if self.debug_blink:
                self.debug_blink.stop()
                self.debug_blink.join()
                self.debug_blink = None
        elif sel == 3:
            if self.info_blink:
                self.info_blink.stop()
                self.info_blink.join()
                self.info_blink = None
        elif sel == 4:
            if self.trace_blink:
                self.trace_blink.stop()
                self.trace_blink.join()
                self.trace_blink = None
        evt.Skip()